/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package appClient;

/**
 *
 * @author User
 */
import clientListener.AppClientListener;
import clientListener.ClientListener;
import com.google.gson.Gson;
import engine.ClientFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.parser.ParseException;
import protocol.ClientProtocol;
import protocol.Command;
import webClient.HttpClient.SocketProcessor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import server.Port;


public class ConnectClient extends Thread {
  private int port;
  private String login; 
  private String pass; 
  private String addr;
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  public javax.swing.JPanel FieldPannel;
  public VisualClien vis;
  public ClientProtocol clientProtocol;
  public String role;
  public String Uid;
  public ClientListener clilist;
  public SocketProcessor socProc;


  
  
  public ConnectClient(String login, String pass, String typeClient) throws IOException{
    
    this.login = login;
    this.pass = pass;
    this.addr = addr;
    //addr = "localhost";

    BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src\\SpringXMLConfig.xml"));
    server.Port ports = (server.Port) factory.getBean("port");
    port = ports.getPort();
    addr = ports.getAddr();
    
   // addr = "169.254.28.1";
   // port = 4510;
    
    System.out.println("addr = " + addr);
    socket = new Socket(addr, port);
    System.out.println("socket = " + socket);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
    clilist = ClientFactory.getClient(typeClient);
    
    start();
    
  }
  
  
  
  public void refuse(String send){
     out.println(send);
  }
  
   
  public void run()
  {
      
    ClientProtocol cl = new ClientProtocol(this);
//      EventProducer ep = new EventProducer();
//      ep.addMyListener(new MyRealListener(this));
    
    try {
          Command obj = new Command();
          obj.Type = "Connect";
          obj.Data = login;
          obj.Add = pass;
          Gson gson = new Gson();
          String send = gson.toJson(obj);
      out.println(send);
      
      while (true) {
        String str = null;
        int length = 7;
        char[] bytes = new char[(int) length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
          offset += numRead;
        }
        String s = new String(bytes);
        length = Integer.parseInt(s);
        System.out.println(s + " leng");
        bytes = new char[(int) length];
        offset = 0;
        numRead = 0;
        while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
          offset += numRead;
        }
        str = new String(bytes);
//        while ( offset != 10){
//          offset = in.read();
//        }

        System.out.println("Client comming string: " + str);

        if (str.equals("{\"Type\": \"End\"}")) {
          out.println("{\"Type\": \"End\"}");
          break;
        }
        try {
          // ep.getClient(str);
          cl.getClient(str); /////////////
        } catch (ParseException ex) {
          Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
      }

    } catch (IOException ex) {
      Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      System.out.println("closing ConnectClient ...");
      try {
        socket.close();
      } catch (IOException ex) {
        Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

  }

    void setVisual(VisualClien vis) {
        this.vis = vis;
    }

  public void setSocProc(SocketProcessor socProc) {
    this.socProc = socProc;
  }
  
}
